//
//  ViewController.m
//  dbTest
//
//  Created by yan on 2017/5/19.
//  Copyright © 2017年 yan. All rights reserved.
//

#import "ViewController.h"
#import "HYDatabaseManager.h"
#import "Model.h"

@interface ViewController ()

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    NSLog(@"%@", NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject);
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 100, 100);
    btn.backgroundColor = [UIColor redColor];
    [btn addTarget:self action:@selector(dbTest) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}


- (void)dbTest {
    [self presentViewController:[[ViewController alloc] init] animated:YES completion:nil];
    NSString *sql = @"CREATE TABLE IF NOT EXISTS data ('id' INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL  UNIQUE, 'start' DATETIME, 'end' DATETIME);";
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSMutableArray *array = [NSMutableArray array];
        [array addObject:sql];
        for (int i = 0; i < 10000; i++) {
            [array addObject:@"INSERT into data (start, end) VALUES (1, 2)"];
        }
        [HYDatabaseManager executeSqls:[array copy] finished:^(BOOL result) {
            NSLog(@"finished");
        }];
        double date = [[NSDate date] timeIntervalSince1970];
        [HYDatabaseManager selectSql:@"SELECT * FROM data" keys:@[@"start"] finished:^(NSArray *results) {
            double date2 = [[NSDate date] timeIntervalSince1970];
            NSLog(@"diff:%f", date2 - date);
        }];
        [HYDatabaseManager selectSql:@"SELECT * FROM data limit 20" class:Model.class keys:@[@"start"] finished:^(NSArray *results) {
            for (Model *model in results) {
                NSLog(@"%@", model.start);
            }
        }];
    });
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
